package com.xc.freeapp.account.controller;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.xc.freeapp.bean.AuthInfo;
import com.xc.freeapp.common.CheckUtils;
import com.xc.freeapp.entity.Collectdoctor;
import com.xc.freeapp.entity.Doctor;
import com.xc.freeapp.entity.DoctorIsCollect;
import com.xc.freeapp.exception.BaseException;
import com.xc.freeapp.service.DoctorService;
import com.xc.freeapp.util.AuthUtil;
import com.xc.freeapp.util.Constant;
import com.xc.freeapp.util.JsonUtil;
import com.xc.freeapp.util.TokenAnnotation;
import com.xc.freeapp.util.UploadUtil;

@Controller
@RequestMapping("/doctor")
@Api(value="/doctor",description = "医院医生")
public class DoctorController extends AbstractController{

	private Logger log = Logger.getLogger(this.getClass());

	@Autowired
	private DoctorService doctorservice;

	/**
	 * 查询医生以及排班信息
	 */
	@TokenAnnotation(required=false)
	@RequestMapping(value = "", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "根据医生id查询医生个人信息以及排班信息")
	public Map<String, Object> getDoctorDetail(@RequestParam("doctorid") String doctorid)
			throws BaseException, ParseException {

		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());

		CheckUtils.notBlankByName(Constant.VER_NOTNULL_BYNAME, doctorid,
				"doctorid");
		Map<String, Object> map = doctorservice.getTakeMedicList(doctorid,
				authInfo.getUserId());
		
		return map;
	}

	/**
	 * 查询医生以及排班信息(增加show显示)
	 */
	@TokenAnnotation(required=false)
	@RequestMapping(value = "/workshow", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "查询医生以及排班信息(增加show显示)")
	public Map<String, Object> getDoctorWork(@RequestParam("doctorid") String doctorid)
			throws BaseException, ParseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		
		CheckUtils.notBlankByName(Constant.VER_NOTNULL_BYNAME, doctorid,
				"doctorid");
		Map<String, Object> map = doctorservice.getWorkTimeList(doctorid,
				authInfo.getUserId());
		
		return map;
	}

	/**
	 * 好医生列表
	 */
	@TokenAnnotation(required=false)
	@RequestMapping(value = "/bydept", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "好医生列表")
	public List<Doctor> getPerfectDorctorByDept(@RequestParam("deptid") String deptid,
			@RequestParam(value = "ifexper", required = false, defaultValue="") String ifexper)
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		
		log.info("好医生" + authInfo.getAppId() + "的医院获取列表");
		List<Doctor> doctorlist = doctorservice
				.getdoctorlist(deptid, authInfo.getAppId(), authInfo.getUserId(), ifexper);
		for (Doctor doctor2 : doctorlist) {
			doctor2.setHeadpic(UploadUtil.urlConversion(doctor2.getHeadpic()));
		}
		
		return doctorlist;
	}
	

	/**
	 * 好医生搜索列表
	 */
	@TokenAnnotation(required=false)
	@RequestMapping(value = "/bykeyword", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "好医生搜索列表")
	public List<Doctor> getdoctorbykeywordS(@RequestParam("keyword") String keyword)
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		CheckUtils.notBlankByName(Constant.VER_NOTNULL_BYNAME, keyword,
				"keyword");
		log.info("好医生" +authInfo.getAppId()+ "的医院搜索列表");
		List<Doctor> doctorlist = doctorservice.getdoctorbykeyword(keyword,
				authInfo.getAppId());
		for (Doctor doctor2 : doctorlist) {
			doctor2.setHeadpic(UploadUtil.urlConversion(doctor2.getHeadpic()));
		}
		return doctorlist;
	}

	/**
	 * 获取收藏专家
	 * 
	 * @param request
	 * @return
	 */
	@TokenAnnotation(required=false)
	@RequestMapping(value = "/getCollectDoctor", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取收藏专家")
	public List<Doctor> getCollectDoctor()
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());

		PageBounds pageBounds = new PageBounds(0, 100);
		return doctorservice.getCollectDoctor(authInfo.getUserId(),pageBounds);
	}
}
